﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Rose.Models;
using Rose.Helper;


namespace Rose.Movies
{
    internal class CameraModel
    {
        public int id { get; set; }
        public string name { get; set; }
    }

    public class CollectMovieDataHelpModel
    {

        private string GetWuHANCameraInfo(string source)
        {
            HtmlTag help = new HtmlTag();
            return help.GetIndexTag(source, "a", 0);

        }

        private string GetCameraHref(string from)
        {
            HtmlTag help = new HtmlTag();
            return help.GetInfoFromSampleTag(from);
        }

        private string GetCameraTitle(string from)
        {
            HtmlTag help = new HtmlTag();
            return help.GetInfoFromSampleTag(from, "title=\"", "\"").Replace("title=", "");
        }

        private int GetCameraId(string fromGetCameraHref)
        {
            //from should be from the 
            string[] splits = fromGetCameraHref.Split('/');
            if (splits[splits.Length - 1] == "")
            {
                return int.Parse(splits[splits.Length - 2]);
            }
            else
            {
                return int.Parse(splits[splits.Length - 1]);
            }

        }




        public void CollectData(Camera camera, string savepath)
        {

            WormShareContentHelpModel share = new WormShareContentHelpModel();
            DownloadAndAddData adddata = new DownloadAndAddData();
            HtmlTag htmlhelp = new HtmlTag();


            string source = share.GetUft8Html(camera.Href);

            string movietag = htmlhelp.GetUniqTag(source, "div", "id=\"theaterShowtimeListDiv\"");

            List<string> saveModel = htmlhelp.GetTagCollect(source, "div", "class=\"table mt25\"");

          
            DateTime today = DateTime.Today;

            string daypath ="\\"+ today.Year + "\\" + today.Month + "\\" + today.Day + "\\";

            foreach (string item in saveModel)
            {
                List<string> imagelist = htmlhelp.GetImageSrc(item);
                List<string> localimagepath = adddata.DownloadImageList(imagelist, savepath + daypath + camera.CameraId + @"\");
 

                if (imagelist.Count()>0)
                {
                    int beginIndex = localimagepath[0].IndexOf("MovieImages");

                    string serverimagepath = localimagepath[0].Substring(beginIndex);

                    List<string> prehttpsrc = htmlhelp.GetImageSrc(item);
                    if (prehttpsrc.Count > 0)
                    {
                        string newitem = item.Replace(prehttpsrc[0], serverimagepath);
                        adddata.AddMovyToDatabase(newitem,camera.CameraId);
                        Console.WriteLine("down ok");
                    }

                }


            }

        }
    }
}